package cn.strategy.fund;

import cn.stock.util.MyUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取板块中股票的资金是持续流入的
 * */
public class Inflow_stock_1 {

    /**
     * 1、找出板块中，资金流入，但是股票没有怎么涨的
     * */
    public static List<String> inflow(String type_name, String last_day) {
        List<String> lst = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT SYMBOL, name FROM TYPE_STOCK WHERE TYPE_NAME = '"+ type_name +"'");
            rs = stmt.executeQuery();
            while (rs.next()) {
                String symbol = rs.getString(1);
                Boolean vs = getStockSymbolFund(symbol, MyUtil.toStr(TypeFund.getDay(last_day, 4)), last_day);
                if (vs) {
                    lst.add(rs.getString(2));
                }
                //ht.put(rs.getString(2), vs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return lst;
    }

    public static Boolean getStockSymbolFund(String symbol, String begin, String end) {

        Boolean flag = false;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        List<List<Double>> lst = new ArrayList<>();

        try {
            conn = MyUtil.getConnection();
            stmt = conn.prepareStatement("SELECT m.zhangdie, m.main_fund, m.main_fund_bf FROM df_stock_date m WHERE m.SYMBOL = ? AND m.STOCK_DATE BETWEEN ? AND ? " +
                    "ORDER BY m.STOCK_DATE desc");
            stmt.setString(1, symbol);
            stmt.setDate(2, MyUtil.toDate(begin));
            stmt.setDate(3, MyUtil.toDate(end));
            rs = stmt.executeQuery();
            while (rs.next()) {
                List<Double> row = new ArrayList<>();
                row.add(rs.getDouble(1));
                row.add(rs.getDouble(2));
                row.add(rs.getDouble(3));
                lst.add(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (lst.size() > 0) {
            List<Double> r1 = lst.get(0);
            if (r1.get(0) > 0D && r1.get(0) < 1.5D && r1.get(1) > 0D) {
                boolean f = true;
                for (int i = 1; i < lst.size(); i++) {
                    List<Double> r = lst.get(i);
                    if (r.get(0) > 0) {
                        f = false;
                        break;
                    }
                }

                if (f == true) {
                    flag = true;
                }
            }

        }
        return flag;
    }
}















































